<?php 
define('DB_SERVER', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'im_260702073');

class DbUtils
{
	public $db_con;
	
	public function connect()
	{
		/* Last parameter allows us to create new database link every time. */
		$this->db_con = mysql_connect(DB_SERVER, DB_USER, DB_PASS, true);
		
		if( !$this->db_con )
		{
			return false;
		}else{
			$select_result = mysql_select_db(DB_NAME);
			if( !$select_result )
			{
				return false;
			}else{
				return true;
			}
		}
	}
	
	public function close()
	{
		if( !$this->db_con )
		{
			return false;
		}
		
		return mysql_close($this->db_con);
	}
	
	public function query( $sql )
	{
		if( !$this->db_con )
		{
			return false;
		}
		
		return mysql_query($sql, $this->db_con);
	}
	
	public function set_var( $var )
	{
		if( !$this->db_con )
		{
			return false;
		}
		
		return $this->quote_smart($var, $this->db_con);
	}
	
	/*
	 * Original source of below function: http://www.homeandlearn.co.uk/php/php14p2.html
	 */
	private function quote_smart($value, $handle) {
		if (get_magic_quotes_gpc()) {
			$value = stripslashes($value);
		}
		if (!is_numeric($value)) {
			$value = "'" . mysql_real_escape_string($value, $handle) . "'";
		}
		return $value;
	}
}
?>